/**
 * Copyright (c) 2021 Choice, Inc.
 * All Rights Reserved.
 * Choice Proprietary and Confidential.
 */
package cn.ruleengine.web.controller;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.date.DateUtil;
import cn.ruleengine.common.vo.BaseResult;
import cn.ruleengine.common.vo.PlainResult;
import cn.ruleengine.web.mq.RuleSetChangeProducer;
import cn.ruleengine.web.service.RuleEngineActionService;
import cn.ruleengine.web.vo.generalrule.ConsensusRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 互认控制器
 *
 * @author <a href="mailto:danliang@myweimai.com">丹良</a>
 * @date 2021/7/28
 */
@Slf4j
@Api(tags = "互认控制器")
@RestController
@RequestMapping("ruleEngine/consensus")
public class ConsensusController {

    @Resource
    private RuleEngineActionService ruleEngineActionService;
    @Resource
    private RuleSetChangeProducer ruleSetChangeProducer;

    @GetMapping("mq")
    public Boolean sendMsg() {
        ruleSetChangeProducer.broadcast("sendMsg", null);
        return Boolean.TRUE;
    }

    @PostMapping("batch")
    @ApiOperation("模拟运行")
    public BaseResult batch(@RequestBody ConsensusRequest request) {
        long ns = System.nanoTime();
        String uuid = UUID.randomUUID().toString();
        log.info("=====> ConsensusController.batch uuid:{}, request:{}", uuid, request);
        if (request == null) {
            return BaseResult.err(-1, "请求不可为空");
        }
        String appid = "consensus-wm";
        String appsecret = "6b3b423@5bc3@142!8^8ddb$4745349$";
        // String appsecret = "6b3J423@5bG3@142!8^8Ydb$4KK5349$";
        if (!Objects.equals(appid, request.getAppid()) ||
                !Objects.equals(appsecret, request.getAppsecret())) {
            return BaseResult.err(-1, "非法请求");
        }

        List<ConsensusRequest.It> its = request.getItems();
        if (CollectionUtils.isEmpty(its)) {
            return BaseResult.err(-1, "请求条目不可为空");
        }
        for (ConsensusRequest.It it : its) {
            String shareCode;
            if (StringUtils.isEmpty(shareCode = it.getShareCode())) {
                return BaseResult.err(-1, "请求互认编码不可为空");
            }
            Integer gender = it.getGender();
            if (null == gender) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中性别不可为空");
            }
            if (gender != 1 && gender != 2 && gender != 3) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中性别非法，性别 1-男 2-女 3-未知");
            }

            if (null == it.getAge() || null == it.getAgeUnit()) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中年龄信息不可为空");
            }
            String ageUnit = it.getAgeUnit();
            if (!Objects.equals("岁", ageUnit)
                    && !Objects.equals("月", ageUnit)
                    && !Objects.equals("天", ageUnit)) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中年龄信息不可为空");
            }

            if (StringUtils.isEmpty(it.getIsMedicalCommunity())) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中是否是医共体不可为空");
            }

            Integer orgLevel = it.getOrgLevel();
            if (StringUtils.isEmpty(orgLevel)) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中当前就诊的医院机构等级不可为空");
            }
            if (orgLevel < 1 || orgLevel > 13) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中当前就诊的医院机构等级为非法数据");
            }

            if (StringUtils.isEmpty(it.getDepartmentGbCode())) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中当前就诊的医院国家标准科室代码不可为空");
            }

            String reportTime = it.getReportTime();
            if (StringUtils.isEmpty(reportTime)) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中报告时间不可为空");
            }
            try {
                DateUtil.parse(reportTime, "yyyy-MM-dd HH:mm:ss");
            } catch (Exception e) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中报告时间不合法");
            }

            if (StringUtils.isEmpty(it.getReportAbnormalIndicator())) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中报告结果正常标记不可为空");
            }

            Integer reportType;
            if (StringUtils.isEmpty(reportType = it.getReportType())) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中报告类型不可为空");
            }
            if (reportType != 1 && reportType != 2) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中报告类型非法");
            }

            Integer reportOrgLevel = it.getReportOrgLevel();
            if (StringUtils.isEmpty(reportOrgLevel)) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中报告机构级别不可为空");
            }
            if (reportOrgLevel < 1 || reportOrgLevel > 13) {
                return BaseResult.err(-1, "请求条目" + shareCode + "中报告机构级别为非法数据");
            }
        }

        if (its.size() > 50) {
            return BaseResult.err(-1, "批量上线最多允许50条");
        }
        PlainResult<Object> plainResult = new PlainResult<>();
        plainResult.setData(ruleEngineActionService.action(its));
        log.info("=====> ConsensusController.batch uuid:{}, cost:{}ns, request:{}",
                uuid, System.nanoTime() - ns, request);
        return plainResult;
    }

}
